#include "timewidget.h"
//w=405  h=160

const int m_monthday[] = {31,28,31,30,31,30,31,31,30,31,30,31};

TimeWidget::TimeWidget(QWidget *parent) : QWidget(parent)
{
    this->setMinimumSize(405, 160);

    m_dateTime = QDateTime::currentDateTime();

    m_yearRolling = new RollingBox(this);
    m_yearRolling->setDirection(RollingBox::SD_VERTICAL);
    m_yearRolling->setRange(m_minYear, m_maxYear);
    m_yearRolling->setValue(m_dateTime.date().year());
    QObject::connect(m_yearRolling, &RollingBox::valueChangedSign, this, &TimeWidget::valueChangeSlot);

    m_ymLabel = new QLabel("-", this);
    m_ymLabel->show();

    m_monthRolling = new RollingBox(this);
    m_monthRolling->setDirection(RollingBox::SD_VERTICAL);
    m_monthRolling->setRange(1, 12);
    m_monthRolling->setValue(m_dateTime.date().month());
    QObject::connect(m_monthRolling, &RollingBox::valueChangedSign, this, &TimeWidget::valueChangeSlot);

    m_mdLabel = new QLabel("-", this);
    m_mdLabel->show();

    m_dayRolling = new RollingBox(this);
    m_dayRolling->setDirection(RollingBox::SD_VERTICAL);
    setMaxDay(m_dateTime.date().year(), m_dateTime.date().month());
    m_dayRolling->setValue(m_dateTime.date().day());
    QObject::connect(m_dayRolling, &RollingBox::valueChangedSign, this, &TimeWidget::valueChangeSlot);

    m_hourRolling = new RollingBox(this);
    m_hourRolling->setDirection(RollingBox::SD_VERTICAL);
    m_hourRolling->setRange(0, 23);
    m_hourRolling->setValue(m_dateTime.time().hour());
    QObject::connect(m_hourRolling, &RollingBox::valueChangedSign, this, &TimeWidget::valueChangeSlot);

    m_hmLabel = new QLabel(":", this);
    m_hmLabel->show();

    m_minRolling = new RollingBox(this);
    m_minRolling->setDirection(RollingBox::SD_VERTICAL);
    m_minRolling->setRange(0, 59);
    m_minRolling->setValue(m_dateTime.time().minute());
    QObject::connect(m_minRolling, &RollingBox::valueChangedSign, this, &TimeWidget::valueChangeSlot);

    m_msLabel = new QLabel(":", this);
    m_msLabel->show();

    m_secRolling = new RollingBox(this);
    m_secRolling->setDirection(RollingBox::SD_VERTICAL);
    m_secRolling->setRange(0, 59);
    m_secRolling->setValue(m_dateTime.time().second());
    QObject::connect(m_secRolling, &RollingBox::valueChangedSign, this, &TimeWidget::valueChangeSlot);
}

void TimeWidget::setDateTime(const QDateTime &dateTime)
{
    if (dateTime.date().year() < m_minYear || dateTime.date().year() > m_maxYear)
        return ;
    m_yearRolling->setValue(dateTime.date().year());
    m_monthRolling->setValue(dateTime.date().month());
    m_dayRolling->setValue(dateTime.date().day());
    m_hourRolling->setValue(dateTime.time().hour());
    m_minRolling->setValue(dateTime.time().minute());
    m_secRolling->setValue(dateTime.time().second());
}

void TimeWidget::resizeEvent(QResizeEvent *event)
{
    int xPos = 0;
    int height = event->size().height();
    int yearWidth = event->size().width() / 6;
    if (yearWidth < 70)
        yearWidth = 70;

    m_yearRolling->resize(yearWidth, height);
    m_yearRolling->move(xPos, 0);

    xPos += m_yearRolling->size().width();
    m_ymLabel->move(xPos + 5, (height / 2) - 10);

    xPos += 15;
    m_monthRolling->resize(yearWidth * 3 / 4, height);
    m_monthRolling->move(xPos, 0);

    xPos += m_monthRolling->size().width();
    m_mdLabel->move(xPos + 5, (height / 2) - 10);

    xPos += 15;
    m_dayRolling->resize(yearWidth * 3 / 4, height);
    m_dayRolling->move(xPos, 0);

    xPos += 15;
    xPos += m_dayRolling->size().width();
    m_hourRolling->resize(yearWidth * 3 / 4, height);
    m_hourRolling->move(xPos, 0);

    xPos += m_hourRolling->size().width();
    m_hmLabel->move(xPos + 5, (height / 2) - 10);

    xPos += 15;
    m_minRolling->resize(yearWidth * 3 / 4, height);
    m_minRolling->move(xPos, 0);

    xPos += m_minRolling->size().width();
    m_msLabel->move(xPos + 5, (height / 2) - 10);

    xPos += 15;
    m_secRolling->resize(yearWidth * 3 / 4, height);
    m_secRolling->move(xPos, 0);

    qDebug()<<"w="<<xPos + m_secRolling->size().width()<<"h="<<height;
}

void TimeWidget::valueChangeSlot(int value)
{
    RollingBox * box = static_cast<RollingBox *>(this->sender());
    if (box == m_yearRolling)
    {
        if (value == m_dateTime.date().year())
            return ;
        int month = m_dateTime.date().month();
        setMaxDay(value, month);
        int day   = m_dateTime.date().day();
        m_dateTime.setDate(QDate(value, month, day));
        qDebug()<<"year value="<<value;
        emit valueChangeSign();
    }
    else if (box == m_monthRolling)
    {
        if (value == m_dateTime.date().month())
            return ;
        int year = m_dateTime.date().year();
        setMaxDay(year, value);
        int day   = m_dateTime.date().day();
        m_dateTime.setDate(QDate(year, value, day));
        qDebug()<<"month value="<<value;
        emit valueChangeSign();
    }
    else if (box == m_dayRolling)
    {
        if (value == m_dateTime.date().day())
            return ;
        int year = m_dateTime.date().year();
        int month   = m_dateTime.date().month();
        m_dateTime.setDate(QDate(year, month, value));
        qDebug()<<"day value="<<value;
        emit valueChangeSign();
    }
    else if (box == m_hourRolling)
    {
        if (value == m_dateTime.time().hour())
            return ;
        int min = m_dateTime.time().minute();
        int sec = m_dateTime.time().second();
        m_dateTime.setTime(QTime(value, min, sec));
        qDebug()<<"hour value="<<value;
        emit valueChangeSign();
    }
    else if (box == m_minRolling)
    {
        if (value == m_dateTime.time().minute())
            return ;
        int hour = m_dateTime.time().hour();
        int sec = m_dateTime.time().second();
        m_dateTime.setTime(QTime(hour, value, sec));
        qDebug()<<"min value="<<value;
        emit valueChangeSign();
    }
    else if (box == m_secRolling)
    {
        if (value == m_dateTime.time().second())
            return ;
        int hour = m_dateTime.time().hour();
        int min = m_dateTime.time().minute();
        m_dateTime.setTime(QTime(hour, min, value));
        qDebug()<<"sec value="<<value;
        emit valueChangeSign();
    }
}

int TimeWidget::monthToDay(int year, int month)
{
    if (month == 2) {
        return (((year%4 == 0) && ((year%100 != 0) || (year%400 == 0)))? 29: 28);
    }else{
        return(m_monthday[month-1]);
    }
}

void TimeWidget::setMaxDay(int year, int month)
{
    int maxDay = monthToDay(year, month);
    m_dayRolling->setRange(1, maxDay);
    if (m_dateTime.date().day() > maxDay)
        m_dateTime.setDate(QDate(year, month, maxDay));
}
